<META http-equiv="refresh" content="<?php echo rand(20,40); ?>" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

$matchId = $_GET["matchId"];
$matchOptaId = @ $_GET["matchOptaId"];
if ($matchId == "") {
	return;
}

//连接数据库
$config = require 'conf/config.ini.php'; //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

//先清空当前比赛场次的数据
$sql = "delete from live_score where matchId = '" . $matchId . "';";
$sql .= "INSERT INTO live_score (scoreId, matchId, homeTeamScore, visitTeamScore, scoreTime, scoreAction) VALUES";

$i = 0;
$id = 0;
$match_s1 = 0;
$match_s2 = 0;
$match_m = null;
$match_st = null;
$match_st_m = null;
$match_st_s = null;
$match_st_t = null;
$match_show_m = null;
$match_show_st = null;
$match_show_s1 = null;
$match_show_s2 = null;

$url = "http://sports.sinajs.cn/livecast/log/log_" . $matchId . ".js?" . rand();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
$matchDetail = curl_exec($ch);
curl_close($ch);

if ($matchDetail == "show_log([])") {
	$matchDetail = file_get_html("http://match.sports.sina.com.cn/livecast/9/iframe/scroll_iframe.php?opta_id=" . $matchOptaId);
	$matchDetail = $matchDetail->find('table tr');

	$sql_values = array ();

	foreach ($matchDetail as $match_tr) {

		//不插入比赛时间是空的内容
		if ($match_tr->find('th', 0)->innertext == "") {
			continue;
		} else {
			$match_st = $match_tr->find('th', 0)->innertext;
			$match_st = str_replace("'", "", $match_st);
			if (strpos($match_st, "+")) {
				$match_st = explode("+", $match_st);
				$match_st = $match_st[0] + $match_st[1];
			}
			$match_st = $match_st . "分";
			$s = $match_tr->find('td', 1)->innertext;
			$s = explode("-", $s);

			//if ($match_st_t != $match_st || $match_s1 != $s[0] || $match_s2 != $s[1]) {

			$match_st_t = $match_st;

			$match_m = $match_tr->find('td', 0)->innertext;
			$match_m = str_replace('<font color="#ff0000">', "", $match_m);
			$match_m = str_replace('</font>', "", $match_m);
			$match_m = str_replace('<strong>', "", $match_m);
			$match_m = str_replace('</strong>', "", $match_m);

			//不插入半角'，插入全角‘
			$match_m = str_replace("'", "’", $match_m);

			$match_s1 = $s[0];
			$match_s2 = $s[1];

			if ($match_show_m == null) {
				$match_show_m = $match_m;
			}
			if ($match_show_st == null) {
				$match_show_st = $match_st;
			}
			if ($match_show_s1 == null) {
				$match_show_s1 = $match_s1;
			}
			if ($match_show_s2 == null) {
				$match_show_s2 = $match_s2;
			}

			$sql_values[$id++] = "'" . $matchId . "', '" . $match_s1 . "','" . $match_s2 . "', '" . $match_st . "', '" . $match_m . "'),";
			//}
		}
	}

	//重新排序比赛.
	foreach ($sql_values as $i => $value) {
		$sql .= "('" . ($id - $i) . "'," . $value;
	}

	echo "<table width='100%'>";
	echo "<tr>";
	echo "<td>" . $id . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_show_st . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_show_s1 . " : " . $match_show_s2 . "</td>";
	echo "</tr><tr>";
	echo "<td>" . $match_show_m . "</td>";
	echo "</tr>";
	echo "</table>";
} else {
	$matchDetail = iconv("GBK", "UTF-8", $matchDetail);
	$matchDetail = str_replace("show_log(", "", $matchDetail);
	$matchDetail = str_replace("}])", "}]", $matchDetail);
	$matchDetail = json_decode($matchDetail);

	foreach ($matchDetail as $match) {
		if (isset ($match->q)) {

			if (strpos($match->m, "...") === 0) {
				$match->m = str_replace("...", "", $match->m);
			}
			//不插入第一位是.的内容
			if (strpos($match->m, ".") === 0) {
				continue;
			}
			//不插入含有：的内容
			if (strpos($match->m, ":") > 0) {
				continue;
			}
			//不插入包含图片的内容
			if (strpos($match->m, "img") > 0) {
				continue;
			}
			//不插入半角'，插入全角‘
			$match->m = str_replace("'", "’", $match->m);
			$match->m = str_replace('<font color=red>', "", $match->m);
			$match->m = str_replace('</font>', "", $match->m);
			$match->m = str_replace('<b>', "", $match->m);
			$match->m = str_replace('</b>', "", $match->m);

			$match_m = $match->m;
			if ($match->q == "0") {
				$match->st = "未赛";
			} else
				if ($match->q == "5") {
					$match->st = "完赛";
				} else {

					//if ((floor($match->st / 60) . "分") != $match_st_m || $match_s1 != $match->s->s1 || strpos($match->m, "结束") || strpos($match->m, "开始")) {

					$match_st_m = floor($match->st / 60) . "分";
					$match_st_s = $match->st % 60;
					$match->st = $match_st_m . $match_st_s;
					if ($match->q == "1") {
						$match->st .= "\n上半场";
					} else
						if ($match->q == "2") {
							$match->st .= "\n下半场";
						} else
							if ($match->q == "3") {
								$match->st .= "\n加时赛";
							} else
								if ($match->q == "4") {
									$match->st .= "\n点球赛";
								}
					$match_st = $match->st;
					$match_s1 = $match->s->s1;
					$match_s2 = $match->s->s2;
					//} else {
					//	continue;
					//}
				}
			$id = $i++;
			$sql .= "('" . $i . "', '" . $matchId . "', '" . $match->s->s1 . "','" . $match->s->s2 . "', '" . $match->st . "', '" . $match->m . "'),";
		}
	}
	echo "<table width='100%'>";
	echo "<tr>";
	echo "<td>" . $id . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_st . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_s1 . " : " . $match_s2 . "</td>";
	echo "</tr><tr>";
	echo "<td>" . $match_m . "</td>";
	echo "</tr>";
	echo "</table>";
}

if ($id > 0) {
	$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";

	//插入比赛比分
	mysqli_multi_query($conn, $sql);
}

/*
 * 暂不采集非直播数据。
 * include_once 'module/simple_html_dom.php';
 * 
if ($matchRecord == "比分板") {
	$matchScoreHTML = file_get_html("http://data.sports.sina.com.cn/live/goal.php?mid=" . $matchId . "&date=" . $matchTime);
	$matchScoreHTML = $matchScoreHTML->find('p');
	$tempMatchInfo = null;
	foreach ($matchScoreHTML as $matchScoreContent) {

		if ($tempMatchInfo != $matchScoreContent->plaintext) {
			$tempMatchInfo = $matchScoreContent->plaintext;
		} else {
			continue;
		}

		$homeTeamScore = false;
		$visitTeamScore = false;

		$scoreTime = explode(iconv("UTF-8", "gb2312", "’"), $matchScoreContent->plaintext);
		$scoreTime = $scoreTime[0];
		if ($matchScoreContent->align == "left") {
			$homeTeamUser = explode(".", $matchScoreContent->plaintext);
			if (count($homeTeamUser) == 1) {
				$homeTeamUser = explode(iconv("UTF-8", "gb2312", "’"), $matchScoreContent->plaintext);
			}
			$homeTeamUser = iconv("gb2312", "UTF-8", $homeTeamUser[1]);
			$visitTeamUser = null;
			$visitTeamScore = true;
		}
		if ($matchScoreContent->align == "right") {
			$visitTeamUser = explode(".", $matchScoreContent->plaintext);
			if (count($visitTeamUser) == 1) {
				$visitTeamUser = explode(iconv("UTF-8", "gb2312", "’"), $matchScoreContent->plaintext);
			}
			$visitTeamUser = iconv("gb2312", "UTF-8", $visitTeamUser[1]);
			$homeTeamUser = null;
			$homeTeamScore = true;
		}
		//红牌
		if (strpos($matchScoreContent->find('img')->src, "bfzb_mj_020.gif")==-1) {
			$scoreAction = "进球";
		}else{
			$scoreAction = "红牌";
		}
		$scoreData[] = array (
			'scoreTime' => $scoreTime,
			'homeTeam' => $homeTeam,
			'visitTeam' => $visitTeam,
			'homeTeamUser' => $homeTeamUser,
			'visitTeamUser' => $visitTeamUser,
			'scoreAction' => $scoreAction,
			'$homeTeamScore' => $homeTeamScore,
			'$visitTeamScore' => $visitTeamScore
		);
	}
}
*/
?>